package com.lx.base.api.dto;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lx.base.api.Pagination;
import com.lx.base.exception.CodeMessage;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.text.MessageFormat;
import java.util.stream.Collectors;

@Data
@ApiModel
@Slf4j
public class BaseDto<T> implements Serializable {
    private static String SUCCESS = "200";
    private String code;
    private T data;
    private String failMsg;
    private Pagination pagination ;
    public BaseDto() {
        pagination=new Pagination();
    }

    public BaseDto(String code, T data) {
        this.code = code;
        this.data = data;
        pagination=new Pagination();
    }

    public static BaseDto success() {
        return new BaseDto(SUCCESS, null);
    }

    public static BaseDto success(Object data) {
        return new BaseDto(SUCCESS, data);
    }

    public static BaseDto success(Object data, Page page) {
        BaseDto baseDto = new BaseDto(SUCCESS, data);
        Pagination pagination = new Pagination();
        pagination.setTotal(page.getTotal());
        pagination.setPageSize(page.getSize());
        pagination.setPageNum(page.getCurrent());
        baseDto.setPagination(pagination);
        return baseDto;
    }

    public static BaseDto success( Page page,Class clazz) {

        Object result= page.getRecords().stream().map(e->{
            Object object=null;
            try {
                object=  clazz.newInstance();
                BeanUtils.copyProperties(e,object);
            }  catch (Exception illegalAccessException) {
                log.error("数据转化异常");
                log.error(illegalAccessException.getMessage());
                return null;
            }
            return object;
        }).collect(Collectors.toList());
        return BaseDto.success(result,page);
    }

    public static BaseDto success( Class clazz,Object res) {

            Object object=null;
            try {
                object=  clazz.newInstance();
                BeanUtils.copyProperties(res,object);
            }  catch (Exception illegalAccessException) {
                log.error("数据转化异常");
                log.error(illegalAccessException.getMessage());
                return null;
            }

        return BaseDto.success(object);
    }


    public static BaseDto success(Object data, Pagination pagination) {
        BaseDto baseDto = new BaseDto(SUCCESS, data);
        baseDto.setPagination(pagination);
        return baseDto;
    }

    public static BaseDto fail(String code, String data) {
        BaseDto baseDto= new BaseDto(code, null);
        baseDto.setFailMsg(data);
        return baseDto;
    }

    public static BaseDto fail(CodeMessage codeMessage) {
        return  BaseDto.fail(codeMessage.getCode(), codeMessage.getMsg());
    }

    public static BaseDto fail(CodeMessage codeMessage,String ext) {
        String msg=  MessageFormat.format(codeMessage.getMsg(), ext);
        return BaseDto.fail(codeMessage.getCode(), msg);
    }

    public Boolean  isSuccess() {
        return  SUCCESS.equals(this.getCode());
    }
}
